package com.hdmall.business.common.util;

import com.hdmall.business.common.common.ResponseCode;
import com.hdmall.business.common.common.ServerResponse;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;

/**
 * Rabbit 推送服务
 */
public class RabbitUtil {
    private static final String  RABBITHOST = "47.98.169.251";
    private static final int  RABBITPORT = 5672;
    private static final String  USESRNAME = "wxy";
    private static final String  USESRPWD = "welLCcome123";

    /**
     * 定位方式
     * @param exchange
     * @param queueName
     * @param routingKey
     * @param message
     */
    public static ServerResponse  rabbitPushForDirect(String exchange, String queueName, String routingKey, String message){
        ConnectionFactory factory= new ConnectionFactory();
        factory.setHost(RABBITHOST);
        factory.setPort(RABBITPORT);

        factory.setUsername(USESRNAME);
        factory.setPassword(USESRPWD);
        Channel channel= null;
        try {
            Connection connection= factory.newConnection();
            channel = connection.createChannel();
            channel.exchangeDeclare(exchange, "direct",true); //direct fanout topic
            channel.queueDeclare(queueName,true,false,false,null);
//            将队列和交换器通过路由绑定
            channel.queueBind(queueName, exchange, routingKey);
            //发送消息
//            channel.basicPublish(, , , , );
            channel.basicPublish(exchange, routingKey, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
            //channel.basicPublish("exchangeName","user001",null,message.getBytes("utf-8"));
            channel.close();
            connection.close();
        } catch (IOException e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.RABBIT_EXCHANGE_ERROR.getCode(),ResponseCode.RABBIT_EXCHANGE_ERROR.getDesc(),null);
        } catch (TimeoutException e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.RABBIT_EXCHANGE_ERROR.getCode(),ResponseCode.RABBIT_EXCHANGE_ERROR.getDesc(),null);

        }
        return ServerResponse.createBySuccess();
    }


    public static ServerResponse  rabbitPushForDirect(String exchange, String routingKey, String message){
        ConnectionFactory factory= new ConnectionFactory();
        factory.setHost(RABBITHOST);
        factory.setPort(RABBITPORT);

        factory.setUsername(USESRNAME);
        factory.setPassword(USESRPWD);
        Channel channel= null;
        try {
            Connection connection= factory.newConnection();
            channel = connection.createChannel();
            channel.exchangeDeclare(exchange, "direct",true); //direct fanout topic
//            channel.queueDeclare(queueName,true,false,false,null);
//            将队列和交换器通过路由绑定
//            channel.queueBind(queueName, exchange, routingKey);
            //发送消息
//            channel.basicPublish(, , , , );
            channel.basicPublish(exchange, routingKey, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
            //channel.basicPublish("exchangeName","user001",null,message.getBytes("utf-8"));
            channel.close();
            connection.close();
        } catch (IOException e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.RABBIT_EXCHANGE_ERROR.getCode(),ResponseCode.RABBIT_EXCHANGE_ERROR.getDesc(),null);
        } catch (TimeoutException e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.RABBIT_EXCHANGE_ERROR.getCode(),ResponseCode.RABBIT_EXCHANGE_ERROR.getDesc(),null);

        }
        return ServerResponse.createBySuccess();
    }


    public static ServerResponse bindQueue(String exchange, String queueName, String routingKey){

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(RABBITHOST);
        factory.setPort(RABBITPORT);
        factory.setUsername(USESRNAME);
        factory.setPassword(USESRPWD);
        Channel channel = null;
        try{
            Connection connection = factory.newConnection();
            channel = connection.createChannel();
            channel.queueDeclare(queueName, false, false, false, null);
            channel.queueBind(queueName, exchange, routingKey);
            channel.close();
            connection.close();
        } catch (TimeoutException | IOException e) {
            e.printStackTrace();
        }

        return ServerResponse.createBySuccess();
    }

    public static ServerResponse rabbitPushForQueue(String exchange, String queueName, String message){

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(RABBITHOST);
        factory.setPort(RABBITPORT);
        factory.setUsername(USESRNAME);
        factory.setPassword(USESRPWD);

        try{
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
//            channel.queueDeclare(queueName, true, false, false, null);
            channel.basicPublish(exchange, queueName, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
            channel.close();
            connection.close();
        } catch (TimeoutException | IOException e) {
            e.printStackTrace();
        }

        return ServerResponse.createBySuccess();
    }


    /**
     * @Date 2018/12/29 12:51
     * @Param [exchange, queueName, routingKey, message, pushWay]
     * @return com.hmall.common.ServerResponse
     * @Description //TODO direct推送
     */
    public static ServerResponse rabbitPushForDirect(String exchange, String queueName, String routingKey, String message, String pushWay){
        ConnectionFactory factory= new ConnectionFactory();
        factory.setHost(RABBITHOST);
        factory.setPort(RABBITPORT);
        factory.setUsername(USESRNAME);
        factory.setPassword(USESRPWD);
        Channel channel= null;
        try {
            Connection connection= factory.newConnection();
            channel = connection.createChannel();
            //设置推送方式
            channel.exchangeDeclare(exchange, pushWay,true); //direct fanout topic
//            channel.queueDeclare(queueName,true,false,false,null);
            //将队列和交换器通过路由绑定
            if(queueName != null) {
                channel.queueBind(queueName, exchange, routingKey);
            }
            //发送消息
            try {
                channel.basicPublish(exchange, routingKey, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
            }catch (Exception e){
                channel.basicPublish(exchange, routingKey, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
            }
            //channel.basicPublish("exchangeName","user001",null,message.getBytes("utf-8"));
            channel.close();
            connection.close();
        } catch (IOException | TimeoutException e) {
            e.printStackTrace();
            return ServerResponse.createByError();
        }
        return ServerResponse.createBySuccess();
    }
    /**
     * 广播方式
     * @param exchange
     * @param routingKey
     * @param message
     */
    public static ServerResponse rabiitPushForPulic(String exchange, String routingKey, String message){
        ConnectionFactory factory= new ConnectionFactory();
        factory.setHost(RABBITHOST);
        factory.setPort(RABBITPORT);

        factory.setUsername(USESRNAME);
        factory.setPassword(USESRPWD);
        Channel channel= null;
        try {
            Connection connection= factory.newConnection();
            channel = connection.createChannel();
            channel.exchangeDeclare(exchange, "fanout",true); //direct fanout topic
            //需要绑定路由键
            channel.basicPublish(exchange, routingKey, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes("utf-8"));
            channel.close();
            connection.close();
        } catch (IOException | TimeoutException e) {
            e.printStackTrace();
            return ServerResponse.createByError();
        }
        return ServerResponse.createBySuccess();

    }
    //删除队列
    public static ServerResponse deleteQueue(String queueName)  {
        ConnectionFactory factory= new ConnectionFactory();
        factory.setHost(RABBITHOST);
        factory.setPort(RABBITPORT);

        factory.setUsername(USESRNAME);
        factory.setPassword(USESRPWD);
        Channel channel= null;
        try {
            Connection connection= factory.newConnection();
            channel = connection.createChannel();
//            channel.exchangeDeclare(exchange, "fanout",true); //direct fanout topic
            channel.queueDelete(queueName);
            channel.close();
            connection.close();
        } catch (IOException | TimeoutException e) {
            e.printStackTrace();
            return ServerResponse.createByError();
        }
        return ServerResponse.createBySuccess();
    }

    public static ServerResponse deleteRoutingKey(String exchange, String routingKey) {

        ConnectionFactory factory= new ConnectionFactory();
        factory.setHost(RABBITHOST);
        factory.setPort(RABBITPORT);

        factory.setUsername(USESRNAME);
        factory.setPassword(USESRPWD);
        Channel channel= null;
        try {
            Connection connection= factory.newConnection();
            channel = connection.createChannel();
//            channel.exchangeDeclare(exchange, "fanout",true); //direct fanout topic

            List<String> queueList = getQueue();
            for(String subQueue : queueList){
                channel.queueUnbind(subQueue, exchange, routingKey);
//               channel.queueDelete(subQueue);
            }

            //
            channel.close();
            connection.close();
        } catch (IOException e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.RABBIT_EXCHANGE_ERROR.getCode(),ResponseCode.RABBIT_EXCHANGE_ERROR.getDesc(),null);
        } catch (TimeoutException e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.RABBIT_EXCHANGE_ERROR.getCode(),ResponseCode.RABBIT_EXCHANGE_ERROR.getDesc(),null);
        }
        return ServerResponse.createBySuccess();


    }

    public static List getQueue() throws IOException {

        String s = HttpKit.Get("http://47.98.169.251:15672/api/queues", "wxy", "welLCcome123");
        int a = s.indexOf("name");
        List<String> str = new ArrayList<>();
        while(a != -1){
            int index = a;
            String queue = "";
            for(int i = index + 7; s.charAt(i) != '\"'; i++){
                queue += s.charAt(i);
            }
            str.add(queue);
            a = s.indexOf("name", a + 1);

        }
        return str;
    }



}
